package com.app.net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkError;
import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.app.util.Utils_Log;

/**
 * @author sinawear 540126629@qq.com
 */
public abstract class NetHelper {

	
	public static final String TAG_URL = "remark url for cancel.";
	
	
	static final String TAG = "NetHelper";
	static boolean DEBUG = true;

	final int SOCKET_TIMEOUT = 10 * 100; // volley 默认2.5s

	// 错误处理
	// http://www.airuanw.com/article/ar3531.html

	// 请求取消参考
	// https://github.com/BoyanAtMentormate/VolleySample/blob/master/VolleySimpleSample/src/main/java/com/example/volleysimplesample/VolleyActivity.java
	
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			if (msg.what == -1) {
				// Utils1.showToastShort(AppUtils.context_app, "网络连接异常!");
				Utils_Log.e(TAG, "sorry,Error: "+"---->"+url);
				String StatusCode = (String) msg.obj;
				OnError(StatusCode);
				return;
			}

			String str = (String) msg.obj;
//			if (DEBUG)
//				System.out.println(str);

			try {

				JSONObject json_obj = new JSONObject(str);
				Utils_Log.e(TAG, url+"---->"+json_obj.toString());
				onComplete(str);
				
			} catch (JSONException e) {

				e.printStackTrace();
				System.out.println("JSONException: " + e.toString());
			}
			finally{
				onCompleteRaw(str);
			}

		};
	};

	// static String strNetRespone =""; 还是发送吧.. 内存不释放...

	// 需要的两个参数
	String url = "";
	ArrayList<BasicNameValuePair> map = new ArrayList<BasicNameValuePair>();

	public abstract String init(ArrayList<BasicNameValuePair> map);

	public abstract void onComplete(String message) throws JSONException; // 并不会强制要函数体内,必须会有json异常跑出呢..

	public void UI_init() {

	}

	public void thread_onComplete(String message) throws JSONException {

	}
	
	public void onCompleteRaw(String message){
		
	}
	
	

	/**
	 * 因为OnError要依赖handler，在AppException中就不能正常了
	 * 
	 * @param StatusCode
	 */
	public void thread_onError(String StatusCode) {

	}

	/**
	 * 返回值两种情况; 1)空字符串，网络完全不通； 2）服务器返回的code;
	 * 
	 * @param StatusCode
	 */
	public void OnError(String StatusCode) {

	}

	/*
	 * public void onRawData(String content) {
	 * 
	 * }
	 */

	private com.android.volley.RequestQueue requestQueue;
	// requestQueue = Volley.newRequestQueue(context);
	// private BaseActivity activity ;
	private Context context;

	public NetHelper(com.android.volley.RequestQueue requestQueue) { //
		// TODO Auto-generated constructor stub

		this.requestQueue = requestQueue;

	}

	public NetHelper(Context context) { // 下面isnull判断context是否为null.如果不是就进入到原始post不使用volley

		this.context = context;
	}

	// http://www.cnblogs.com/devinzhang/archive/2012/01/08/2316453.html
	public void start_raw_GET() {

		UI_init();

		new Thread() {

			public void run() {

				url = init(map);
				if (url.length() == 0)
					return;

				StringBuilder params = new StringBuilder("");
				try {
					if (map != null) {
						params.append("?");
						for (int i = 0; i < map.size(); i++) {
							BasicNameValuePair NameValue = map.get(i);
							// System.out.println("name: "+NameValue.getName() +
							// "value: "+NameValue.getValue());
							params.append(NameValue.getName());
							params.append("=");
							params.append(URLEncoder.encode(
									NameValue.getValue(), "UTF-8"));
							params.append("&");
						}

					}
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Utils_Log.e(TAG, "------------------------------------------------");
				Utils_Log.e(TAG, "url=" + url);
				Utils_Log.e(TAG, "params=" + params);

				url = url + params;
				Utils_Log.e(TAG, "result url=" + url);

				if (context == null) {
					volley_get();
				} else {
					raw_get();
				}

			};
		}.start();

	}

	private void volley_post() {

		StringRequest StringRequest = new StringRequest(Request.Method.POST,
				url, new Response.Listener<String>() {
					@Override
					public void onResponse(String strNetRespone) {
						System.out.println("strNetRespone=" + strNetRespone);

						try {
							thread_onComplete(strNetRespone);

							Message msg = new Message();
							msg.obj = strNetRespone;
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						System.out.println("sorry,Error: " + url);

						String StatusCode = "";

						if (arg0 != null) {
							String message = arg0.getMessage();
							if (message != null)
								System.out.println(message);

							NetworkResponse response = arg0.networkResponse;
							if (response != null)
								StatusCode = "" + response.statusCode;
						}

						thread_onError(StatusCode);

						Message msg = new Message();
						msg.obj = StatusCode;
						msg.what = -1;
						handler.sendMessage(msg);

					}

				}) {
			
			
		
			  // 发送请求
			  @Override 
				public java.util.Map<String, String> getHeaders()
						throws AuthFailureError {

					Map<String, String> headers = new HashMap<String, String>();

					// 发送请求
					//  headers.put("Charset", "UTF-8"); 
					//  headers.put("Content-Type","application/json");
					 
					return headers;

				};
			

			/*
			 * @Override public String getPostBodyContentType() { // TODO
			 * Auto-generated method stub
			 * System.out.println("getPostBodyContentType");
			 * 
			 * return super.getPostBodyContentType(); }
			 * 
			 * @Override protected String getPostParamsEncoding() { // TODO
			 * Auto-generated method stub
			 * 
			 * System.out.println("getPostParamsEncoding");
			 * 
			 * return super.getPostParamsEncoding(); }
			 * 
			 * @Override public byte[] getPostBody() throws AuthFailureError {
			 * // TODO Auto-generated method stub
			 * 
			 * System.out.println("getPostBody"); return super.getPostBody(); }
			 * 
			 * @Override protected Map<String, String> getPostParams() throws
			 * AuthFailureError { // TODO Auto-generated method stub
			 * 
			 * System.out.println("getPostParams");
			 * 
			 * 
			 * Map<String, String> temp_map = new HashMap<String, String>();
			 * 
			 * // 添加body for(int i = 0; i<map.size(); i++){ BasicNameValuePair
			 * pair = map.get(i); String name = pair.getName(); String value =
			 * pair.getValue();
			 * 
			 * temp_map.put(name, value); }
			 * 
			 * return temp_map; }
			 */

			@Override
			public RetryPolicy getRetryPolicy() {
				// TODO Auto-generated method stub
				return super.getRetryPolicy();
			}

			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				// TODO Auto-generated method stub

				Map<String, String> temp_map = new HashMap<String, String>();

				// 添加body
				for (int i = 0; i < map.size(); i++) {
					BasicNameValuePair pair = map.get(i);
					String name = pair.getName();
					String value = pair.getValue();

					temp_map.put(name, value);
				}

				return temp_map;

			}

		};

		try {

			String Headers = StringRequest.getHeaders().toString();
			String body_content_type = StringRequest.getBodyContentType();
			System.out.println("headers: " + Headers);
			System.out.println("body content type: " + body_content_type);

		} catch (AuthFailureError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		
		StringRequest.setTag( TAG_URL);
		requestQueue.add(StringRequest);

	}

	private void volley_get() {

		StringRequest StringRequest = new StringRequest(Request.Method.GET,
				url, new Response.Listener<String>() {

					@Override
					public void onResponse(String strNetRespone) {
						//System.out.println("strNetRespone=" + strNetRespone);

						try {
							thread_onComplete(strNetRespone);

							Message msg = new Message();
							msg.obj = strNetRespone;
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						//System.out.println("sorry,Error: " + url);

						if (error instanceof NetworkError) {
							// } else if( error instanceof ClientError) {
						} else if (error instanceof ServerError) {
						} else if (error instanceof AuthFailureError) {
						} else if (error instanceof ParseError) {
						} else if (error instanceof NoConnectionError) {
							System.out.println("NoConnectionError");
						} else if (error instanceof TimeoutError) {
							System.out.println("TimeoutError");
						}

						String StatusCode = "";

						if (error != null) {
							String message = error.getMessage();
							if (message != null)
								System.out.println(message); // 也可以""+null;
							// 得到返回code 如果是断网状态response返回为null
							NetworkResponse response = error.networkResponse;
							if (response != null)
								StatusCode = "" + response.statusCode;
						}

						Message msg = new Message();
						msg.obj = StatusCode;
						msg.what = -1;
						handler.sendMessage(msg);

					}

				}) {
			/*
			 * public java.util.Map<String, String> getHeaders() throws
			 * AuthFailureError {
			 * 
			 * Map<String, String> headers = new HashMap<String, String>();
			 * 
			 * // 发送请求 return headers;
			 * 
			 * };
			 */

			// http://bUtils_Log.csdn.net/hpb21/article/details/12163371
			@Override
			public RetryPolicy getRetryPolicy() {
				RetryPolicy retryPolicy = new DefaultRetryPolicy(
						SOCKET_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
						DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

				return retryPolicy;
			}

		};

		try {
			String Headers = StringRequest.getHeaders().toString();
			String body_content_type = StringRequest.getBodyContentType();
		//	int TimeoutMs = StringRequest.getTimeoutMs();
			System.out.println("headers: " + Headers);
			System.out.println("body content type: " + body_content_type);
	//		System.out.println("TimeoutMs: " + TimeoutMs);

		} catch (AuthFailureError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

		StringRequest.setTag( TAG_URL );
		requestQueue.add(StringRequest);

	}

	private void volley_json_get() {

		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				Request.Method.GET, url, null,
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						System.out.println("response=" + response);

						try {
							String strNetRespone = response.toString();
							thread_onComplete(strNetRespone);

							Message msg = new Message();
							msg.obj = strNetRespone;
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						System.out.println("sorry,Error");

						Message msg = new Message();
						msg.obj = "";
						msg.what = -1;
						handler.sendMessage(msg);

					}

				}) {
			public java.util.Map<String, String> getHeaders()
					throws AuthFailureError {

				Map<String, String> headers = new HashMap<String, String>();

				// 发送请求
				/*
				 * headers.put("Charset", "UTF-8"); headers.put("Content-Type",
				 * "application/json");
				 * 
				 * if(UserService.isLogin()){ headers.put("mobile_sessionid",
				 * UserService.getUser().getSessionId());
				 * headers.put("username",
				 * UserService.getUser().account.getAccountName());
				 * headers.put("userid",
				 * ""+UserService.getUser().account.getId() ); String mobile =
				 * UserService.getUser().getMobile(); if(mobile != null){
				 * headers.put("mobile",
				 * ""+UserService.getUser().account.getMobile() ); } }
				 * headers.put("access_type", "mobile_front"); headers.put("ip",
				 * AppUtils.ip);
				 */
				return headers;

			};
		};

		try {

			String Headers = jsonObjectRequest.getHeaders().toString();
			String body_content_type = jsonObjectRequest.getBodyContentType();
			System.out.println("headers: " + Headers);
			System.out.println("body content type: " + body_content_type);

		} catch (AuthFailureError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

		requestQueue.add(jsonObjectRequest);

	}

	private void raw_get() {
		try {
			URL u = new URL(url);
			HttpURLConnection urlConn = (HttpURLConnection) u.openConnection();
			urlConn.setConnectTimeout(10000);// 10s内连不上就断开
			// 因为这个是post请求,设立需要设置为true
			urlConn.setDoInput(true);
			urlConn.setUseCaches(false);
			urlConn.setRequestProperty("Charset", "UTF-8");

			// 发送请求
			urlConn.setRequestProperty("Content-Type", "application/json");
			/*
			 * if(UserService.isLogin()){
			 * urlConn.setRequestProperty("mobile_sessionid",
			 * UserService.getUser().getSessionId()); }
			 * urlConn.setRequestProperty("access_type", "mobile_front");
			 * urlConn.setRequestProperty("ip", AppUtils.ip);
			 */

			urlConn.connect();
			// 获服务器取数据
			InputStream inputStream = urlConn.getInputStream();
			byte[] bytes = new byte[4096];
			int length = 0;
			byte[] data = new byte[0];
			byte[] tmpdata = null;
			while (((length = inputStream.read(bytes, 0, 4096))) > 0) {
				tmpdata = new byte[length + data.length];
				System.arraycopy(data, 0, tmpdata, 0, data.length);
				System.arraycopy(bytes, 0, tmpdata, data.length, length);
				data = tmpdata;
			}
			String strNetRespone = new String(data, "utf-8");
			System.out.println(strNetRespone);
			// 关闭http连接
			urlConn.disconnect();

			thread_onComplete(strNetRespone);

			// String strNetRespone = httpGetHelper(map);
			Message msg = new Message();
			msg.obj = strNetRespone;
			msg.what = 0;
			handler.sendMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();

			Message msg = new Message();
			msg.obj = "";
			msg.what = -1;
			handler.sendMessage(msg);
		}
	}

	public void start_raw_POST() {

		UI_init();

		new Thread() {

			public void run() {

				url = init(map);
				if (url.length() == 0)
					return;

				Utils_Log.e(TAG, "------------------------------------------------");
				Utils_Log.e(TAG, "url=" + url);
				Utils_Log.e(TAG, "parameters=" + map.toString());

				if (context == null)
					volley_post();
				else {
					raw_post();
				}

			};
		}.start();

	}

	private void volley_json_post() {

		String str_request = map.get(0).getValue();
		JSONObject json_request = null;
		JSONArray json_array_request = null;
		try {
			json_request = new JSONObject(str_request);
			volley_json_obj_request(json_request);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
		}
		try {
			json_array_request = new JSONArray(str_request);
			// volley_json_array_request(json_array_request);
			raw_post();

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
		}

	}

	private void volley_json_array_request(JSONArray json_request) {

		// *没法设置jsonarraybody; 没见到提供string插入到body呀;

		final String requestBody = json_request.toString();

		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				Request.Method.POST, url, null,
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						System.out.println("response=" + response);

						try {
							String strNetRespone = response.toString();
							thread_onComplete(strNetRespone);

							Message msg = new Message();
							msg.obj = strNetRespone;
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						System.out.println("sorry,Error");

						Message msg = new Message();
						msg.obj = "";
						msg.what = -1;
						handler.sendMessage(msg);

					}

				}) {
			// http://www.cnblogs.com/lee0oo0/archive/2013/10/28/3392035.html
			public java.util.Map<String, String> getHeaders()
					throws AuthFailureError {

				Map<String, String> headers = new HashMap<String, String>();

				// 发送请求
				headers.put("Charset", "UTF-8");
				headers.put("Content-Type", "application/json");
				/*
				 * if(UserService.isLogin()){ headers.put("mobile_sessionid",
				 * UserService.getUser().getSessionId());
				 * headers.put("username",
				 * UserService.getUser().account.getAccountName());
				 * headers.put("userid",
				 * ""+UserService.getUser().account.getId() ); String mobile =
				 * UserService.getUser().getMobile(); if(mobile != null){
				 * headers.put("mobile",
				 * ""+UserService.getUser().account.getMobile() ); } }
				 * headers.put("access_type", "mobile_front"); headers.put("ip",
				 * AppUtils.ip);
				 */

				return headers;

			};

			// protected java.util.Map<String,String> getParams() throws
			// AuthFailureError {};

		};

		requestQueue.add(jsonObjectRequest);

	}

	private void volley_json_obj_request(JSONObject json_request) {
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				Request.Method.POST, url, json_request,
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						System.out.println("response=" + response);

						try {
							String strNetRespone = response.toString();
							thread_onComplete(strNetRespone);

							Message msg = new Message();
							msg.obj = strNetRespone;
							msg.what = 0;
							handler.sendMessage(msg);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						System.out.println("sorry,Error");

						Message msg = new Message();
						msg.obj = "";
						msg.what = -1;
						handler.sendMessage(msg);

					}

				}) {
			// http://www.cnblogs.com/lee0oo0/archive/2013/10/28/3392035.html
			public java.util.Map<String, String> getHeaders()
					throws AuthFailureError {

				Map<String, String> headers = new HashMap<String, String>();

				// 发送请求
				headers.put("Charset", "UTF-8");

				/*
				 * headers.put("Content-Type", "application/json");
				 * 
				 * if(UserService.isLogin()){ headers.put("mobile_sessionid",
				 * UserService.getUser().getSessionId());
				 * headers.put("username",
				 * UserService.getUser().account.getAccountName());
				 * headers.put("userid",
				 * ""+UserService.getUser().account.getId() ); String mobile =
				 * UserService.getUser().getMobile(); if(mobile != null){
				 * headers.put("mobile",
				 * ""+UserService.getUser().account.getMobile() ); } }
				 * headers.put("access_type", "mobile_front"); headers.put("ip",
				 * AppUtils.ip);
				 */

				return headers;

			};

			// protected java.util.Map<String,String> getParams() throws
			// AuthFailureError {};

		};

		requestQueue.add(jsonObjectRequest);
	}

	private void raw_post() {

		try {

			HttpPost request = new HttpPost(url);
			String str_request = map.get(0).getValue();

			StringEntity se = new StringEntity(str_request, HTTP.UTF_8);
			request.setEntity(se);

			// 发送请求
			request.addHeader("Content-Type", "application/json");
		 
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(request);
			// 得到应答的字符串，这也是一个 JSON 格式保存的数据
			String strNetRespone = EntityUtils.toString(
					httpResponse.getEntity(), "utf-8");
			// System.out.println(""+strNetRespone);

			thread_onComplete(strNetRespone);

			Message msg = new Message();
			msg.obj = strNetRespone;
			msg.what = 0;
			handler.sendMessage(msg);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			Message msg = new Message();
			msg.obj = "";
			msg.what = -1;
			handler.sendMessage(msg);

		}

	}


	private String httpGetHelper(ArrayList<BasicNameValuePair> requestParams) {

		HashMap<String, String> parameters = new HashMap<String, String>();
		for (BasicNameValuePair basic : requestParams) {

			parameters.put(basic.getName(), basic.getValue());
		}

		String jsonString = "";
		InputStream inputStream = null;
		try {
			StringBuilder params = new StringBuilder("");
			if (parameters != null) {
				params.append("?");

				for (Map.Entry<String, String> entry : parameters.entrySet()) {
					params.append(entry.getKey());
					params.append("=");
					params.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
					params.append("&");
				}
			}
			BasicHttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
			HttpConnectionParams.setSoTimeout(httpParams, 20000); //
			HttpClient client = new DefaultHttpClient(httpParams);

			HttpGet request = new HttpGet(url + params);
			Utils_Log.e(TAG, "------------------------------------------------");
			Utils_Log.e(TAG, "URL=" + url + params);
			HttpResponse response = client.execute(request);

			HttpEntity httpEntity = response.getEntity();

			int StatusCode = response.getStatusLine().getStatusCode();
			if (DEBUG)
				Utils_Log.e(TAG, "StatusCode=" + StatusCode);

			if (StatusCode == 200) {
				inputStream = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(inputStream));
				String result = "";
				String line = "";
				while ((line = reader.readLine()) != null) {
					result = result + line;
				}
				inputStream.close();
				jsonString = result;
			}

			return jsonString;

		} catch (Exception e) {
			e.printStackTrace();
			Utils_Log.e(TAG, e.toString());

			return "";
		}

	}

	private String httpPostHelper(ArrayList<BasicNameValuePair> parameters) {
		String jsonString = "";
		try {

			HttpPost post = new HttpPost(url);
			Utils_Log.e(TAG, "------------------------------------------------");
			Utils_Log.e(TAG, "url=" + url);
			Utils_Log.e(TAG, "parameters=" + parameters.toString());
			BasicHttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
			HttpConnectionParams.setSoTimeout(httpParams, 1 * 60 * 1000);
			post.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
			HttpResponse response = new DefaultHttpClient(httpParams)
					.execute(post);

			int StatusCode = response.getStatusLine().getStatusCode();
			if (DEBUG)
				Utils_Log.e(TAG, "StatusCode=" + StatusCode);

			if (StatusCode == 200)
				jsonString = EntityUtils
						.toString(response.getEntity(), "UTF-8");
			else {
				thread_onError("" + StatusCode);
			}

			return jsonString;

		} catch (Exception e) {
			e.printStackTrace();
			Utils_Log.e(TAG, e.toString());
			return jsonString;
		}

	}

	public void start_GET() {

		UI_init();

		new Thread() {

			public void run() {

				url = init(map);
				if (url.length() == 0)
					return;

				String strNetRespone = httpGetHelper(map);

				try {
					thread_onComplete(strNetRespone);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Message msg = new Message();
				msg.obj = strNetRespone;
				msg.what = 0;
				handler.sendMessage(msg);

				// onComplete(httpGetHelper(map));

			};
		}.start();

	}

	public void start_POST() {

		UI_init();

		new Thread() {

			public void run() {

				url = init(map);
				if (url.length() == 0)
					return;

				String strNetRespone = httpPostHelper(map);

				try {
					thread_onComplete(strNetRespone);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Message msg = new Message();
				msg.obj = strNetRespone;
				msg.what = 0;
				handler.sendMessage(msg);
				// onComplete(httpPostHelper(map));

			};
		}.start();

	}

}
